package com.leetcode.p100;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * 回溯
 */
public class Test131 {

    private List<List<String>> res = new ArrayList<>();
    private String             s;

    public List<List<String>> partition(String s) {
        if (s.length() <= 0) {
            return res;
        }
        this.s = s;

        Deque<String> stack = new ArrayDeque<>();

        dfs(s.toCharArray(), 0, s.length(), stack);

        return res;
    }

    private void dfs(char[] charArray, int index, int len, Deque<String> stack) {
        if (index == len) {
            res.add(new ArrayList<>(stack));
            return;
        }

        for (int i = index; i < len; i++) {
            if (!checkPalindrome(charArray, index, i)) {
                continue;
            }
            stack.addLast(new String(charArray, index, i + 1 - index));
            dfs(charArray, i + 1, len, stack);
            stack.removeLast();
        }
    }

    private boolean checkPalindrome(char[] charArray, int left, int right) {
        while (left < right) {
            if (charArray[left] != charArray[right]) {
                return false;
            }
            right--;
            left++;
        }
        return true;
    }

    public static void main(String[] args) {
        Test131 test = new Test131();
        System.out.println(test.partition("25525511135"));
    }

}
